package com.sry.blog.sys.controller;


import com.sry.blog.sys.model.ArticleToTag;
import com.sry.blog.sys.model.Tag;
import com.sry.blog.sys.service.ArticleToTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * InnoDB free: 10240 kB; (`article_id`) REFER `blog/article`(`id`) ON UPDATE CASCA 前端控制器
 * </p>
 *
 * @author SR_Y
 * @since 2022-01-05
 */
@RestController
@RequestMapping("/articleToTag")
@ResponseBody
public class ArticleToTagController {

    @Autowired
    private ArticleToTagService articleToTagService;

    @Autowired
    private TagController tagController;


    public boolean create(ArticleToTag articleToTag){
        if(!articleToTagService.isExist(articleToTag))
            return articleToTagService.creatOrUpdate(articleToTag);
        return false;
    }

    @RequestMapping(value = "/isExist",method = RequestMethod.POST)
    public boolean isExist(@RequestBody ArticleToTag articleToTag){
        return articleToTagService.isExist(articleToTag);
    }

    public List<Tag> getTags(Integer id){
        List<ArticleToTag> list = articleToTagService.list(ArticleToTag.builder().articleId(id).build());
        List<Tag> tags = new ArrayList<>();
        for (ArticleToTag articleToTag : list) {
            tags.add(tagController.getTag(articleToTag.getTagId()));
        }
        return tags;
    }
    public List<ArticleToTag> getArticles(Integer tagId){
        List<ArticleToTag> list = articleToTagService.list(ArticleToTag.builder().tagId(tagId).build());
        return list;
    }


}

